package com.john.blog.config.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.john.blog.common.constant.SystemEnum;
import com.john.blog.common.utils.RequestUtils;
import com.john.blog.common.vo.UrlPoint;

/**
 * 基础初始化拦截器
 * 
 * @author JiangGengchao
 * @date 2018年2月8日
 */
@Component("initInterceptor")
public class InitInterceptor extends HandlerInterceptorAdapter {

    @Value("${server.version}")
    private String systemVrsion;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        setSystemAttribute(request);
        String basePath = RequestUtils.getBasePath(request);
        request.setAttribute("base", basePath);
        request.setAttribute("systemversion", systemVrsion);
        return true;
    }

    /**
     * 设置系统名称
     * 
     * @param request
     */
    private void setSystemAttribute(HttpServletRequest request) {
        UrlPoint endPoint = RequestUtils.getUrlEndPoint(request);
        if (endPoint.getContext().indexOf(SystemEnum.BACK.getSystemName()) >= 0) {
            request.setAttribute("system", SystemEnum.BACK.getSystemName());
        } else {
            request.setAttribute("system", SystemEnum.FRONT.getSystemName());
        }
    }

}
